package com.soft.javase.day12.bank;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/19
 */
public class Account {
    private String name;
    private double balance;

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    public synchronized void withdraw(double amount) {
        while (balance < amount) {
            System.out.println(Thread.currentThread().getName() + " 取款失败，余额不足");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        balance -= amount;
        System.out.println(Thread.currentThread().getName() + " 取款 " + amount + " 成功，余额: " + balance);

    }

    public synchronized void deposit(double amount) {
        balance += amount;
        System.out.println(Thread.currentThread().getName() + " 存款 " + amount + " 成功，余额: " + balance);
        notifyAll();
    }
}
